package com.techventus.locations;

import java.util.Set;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LocationDecisionMenu extends Activity{
	
	TextView locationNameTextView;
	TextView gpscoordsTextView;
	TextView phonePrefsStatusTextView;
	TextView awarenessTextView;
	
	Button viewEditLocationButton;
	Button viewEditPhoneButton;
	Button enableDisableButton;
	
	
	String locationName;
	int[] gpscoords = new int[] {0,0};
	int radius = 100;
	boolean awareness;
	int phonePrefsStatus;
	
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); 
        setContentView(R.layout.locationdecisionmenu);
        
        locationNameTextView = (TextView)findViewById(R.id.locationdecisionlocation);
        gpscoordsTextView = (TextView)findViewById(R.id.locationdecisiongpscoords);
        awarenessTextView = (TextView)findViewById(R.id.locationawarenessstatus);
        viewEditLocationButton = (Button)findViewById(R.id.locationdecisionlocationbutton); 
        phonePrefsStatusTextView = (TextView)findViewById(R.id.phoneprefsstatus);
        
        Bundle bundle = getIntent().getExtras();
        Set<String> keys =bundle.keySet();
        Log.e("TECHVENTUS",String.valueOf(keys.size()));
        locationName = bundle.getString("locationName");
        
        
        
        if(locationName.equals("Elsewhere")){
        	viewEditLocationButton.setVisibility(View.INVISIBLE);
        }else{
        	viewEditLocationButton.setVisibility(View.VISIBLE);
        	viewEditLocationButton.setOnClickListener(locationClick);
        }
        
        getLocationVariables();
        
        if(gpscoords[0]!=0||gpscoords[1]!=0)
        	gpscoordsTextView.setText("LAT: "+String.valueOf(((double)gpscoords[0]/(double)1E6))
        			+" LON: "+String.valueOf(((double)gpscoords[1]/(double)1E6)));
        else
        	gpscoordsTextView.setText("LAT: -XXX.XXXXX LON: -XXX.XXXXX");
        
        awarenessTextView .setText(String.valueOf(awareness));
        
        /*
        try{
        	
        	int lat = bundle.getInt("locationLatitude");
        	int lon = bundle.getInt("locationLongitude");
        	System.out.println("lat"+lat);
        	System.out.println("lon"+lon);
        	gpscoords = new int[] {lat,lon};

        }catch(Exception e){
        	e.printStackTrace();
        }
        */
        if(locationName!=null)
        	locationNameTextView.setText(locationName);
        

        viewEditPhoneButton = (Button)findViewById(R.id.locationdecisionphonebutton); 
        
        
        viewEditPhoneButton.setOnClickListener(phoneClick);
       
        
      
        	establishPhonePrefsStatus();
	}
	
	@Override
	public void onResume(){
		
		super.onResume();
		
		Log.e("TECHVENTUS", "RESUMING Location Decision Menu");
		
		if(!phonePrefsStatusTextView.getText().equals("SET")){
				
			  	establishPhonePrefsStatus();
			  	
		}
	}
	
	OnClickListener locationClick = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			Intent i = new Intent(LocationDecisionMenu.this,LocationMap.class);
			Bundle b = new Bundle();
			b.putString("locationName", locationName);
			b.putBoolean("isNew", false);

			SQLiteDatabase sql= openOrCreateDatabase("db",0,null);
			
			Cursor c =sql.rawQuery("SELECT locationLatitude,locationLongitude,locationRadius, enabled FROM LOCATIONS where locationName = '"+locationName+"';", null);
			
			   if(c!=null){
				   c.moveToNext();
				   if(c.isFirst()){
					   //do{
						   b.putInt("latitude", c.getInt(0));
						   b.putInt("longitude", c.getInt(1));
						   b.putInt("radius", c.getInt(2));
						 // b.putString(c.getString(0), c.getString(1)) ;
						   		
			//			   Log.e("TECHVENTUS","Phone added to bundle "+c.getString(0));
						   		
						   //if( c.getString(0).equals("updateLoginCredentials")){}
						   
						   
					  // }while(c.moveToNext());
				   }else{
					   Log.e("TECHVENTUS","NOT FIRST");
				   }
			   }
			c.close();
			sql.close();
			i.putExtras(b);
			startActivity(i);
			
			
			
		}
		
	};
	
	
	
	void getLocationVariables(){
		
		try{
		
		if(!locationName.equals("Elsewhere")){
			
			
		
			SQLiteDatabase sql= openOrCreateDatabase("db",0,null);
			
			Cursor c =sql.rawQuery("SELECT locationLatitude,locationLongitude,locationRadius, enabled FROM LOCATIONS where locationName = '"+locationName+"';", null);
			
			   if(c!=null){
				   c.moveToNext();
				   if(c.isFirst()){
					  // do{
						   gpscoords[0] = c.getInt(0);
						   gpscoords[1] =  c.getInt(1);
						   radius= c.getInt(2);
						   awareness= Boolean.valueOf(c.getString(3));
						 // b.putString(c.getString(0), c.getString(1)) ;
						   		
			//			   Log.e("TECHVENTUS","Phone added to bundle "+c.getString(0));
						   		
						   //if( c.getString(0).equals("updateLoginCredentials")){}
						   
						   
					   //}while(c.moveToNext());
				   }else{
					   Log.e("TECHVENTUS","NOT FIRST");
				   }
			   }
			c.close();
			sql.close();
			
		}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	private void establishPhonePrefsStatus(){
		Log.e("TECHVENTUS","Establish Phone Prefs Status");
		try{
			SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
		
			Cursor c = sql.rawQuery("SELECT phoneEnable FROM LOCATIONPHONEENABLE WHERE locationName = '"+locationName+"';", null);
		
			if(c!=null){
				Log.e("TECHVENTUS","C not null");
				boolean valid = false;
				while(c.moveToNext()){
					Log.e("TECHVENTUS","In the while Check...");
					Log.e("TECHVENTUS","In the while Check phone "+c.getInt(0));
					if(c.getInt(0)!=-2){
						valid = true;
						phonePrefsStatusTextView.setText("SET");
						break;
					}else if(c.getInt(0)==-2){
						valid = true;
					}
				}
				if(!phonePrefsStatusTextView.getText().equals("SET") && valid){
					phonePrefsStatusTextView.setText("NOT SET");
				}else if (!valid){
					phonePrefsStatusTextView.setText("UNKNOWN");
				}
				c.close();
			}else{
				phonePrefsStatusTextView.setText("UNKNOWN");
			}
			sql.close();
		
		}catch(Exception u){
			u.printStackTrace();
		}
	}
	
	
	OnClickListener phoneClick = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			Intent i = new Intent(LocationDecisionMenu.this,PhonePreference.class);
			Bundle b = new Bundle();
			b.putString("locationName", locationName);
			
			SQLiteDatabase sql= openOrCreateDatabase("db",0,null);
			
			Cursor c =sql.rawQuery("SELECT phoneName, phoneEnable FROM LOCATIONPHONEENABLE where locationName = '"+locationName+"';", null);
			
			   if(c!=null){
				   while(c.moveToNext()){
						  b.putInt(c.getString(0), c.getInt(1)) ;
					   		
						   Log.e("TECHVENTUS","Phone added to bundle "+c.getString(0));
				   }
				   c.close();
			   }
			
			sql.close();
			i.putExtras(b);
			startActivity(i);
			
			
			
		}
		
	};

}
